package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CustomerProblemDealListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CustomerProblem;
import com.isunland.managesystem.entity.CustomerProblemDeal;
import com.isunland.managesystem.entity.CustomerProblemDealOriginal;
import com.isunland.managesystem.entity.DataStatus;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能 ：处理列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/2/23 10:52
 */
public class CustomerProblemDealListFragment extends BaseListFragment {
  protected static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.CustomerProblemDealListFragment.EXTRA_CONTENT";

  private final int REQUEST_DETAIL = 0;
  private CustomerProblem mContent;
  private ArrayList<CustomerProblemDeal> mList;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mContent = (CustomerProblem) getArguments().getSerializable(EXTRA_CONTENT);
    mList = new ArrayList<>();
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mListview.setDividerHeight(0);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    //待处理,处理中状态
    if (DataStatus.UNDEAL.equalsIgnoreCase(mContent.getDataStatus())
        || DataStatus.DEALING.equalsIgnoreCase(mContent.getDataStatus())) {
      inflater.inflate(R.menu.menu_add, menu);
    } else {
      super.onCreateOptionsMenu(menu, inflater);
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        return true;
      case R.id.menu_item_add:
        Intent intent = new Intent(getActivity(), CustomerProblemDealActivity.class);
        intent.putExtra(CustomerProblemDealFragment.EXTRA_COMPLAINFAULTID, mContent.getId());
        startActivityForResult(intent, REQUEST_DETAIL);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("complainFaultId", mContent.getId());
    return params;
  }

  @Override public String getUrl() {
    return ApiConst.URL_CUSTOMER_PROBLEM_DEALLIST;
  }

  @Override public void resolveResponse(String json) {
    CustomerProblemDealOriginal original =
        new Gson().fromJson(json, CustomerProblemDealOriginal.class);
    if (original.getResult() == Base.SUCCESS && original.getRows() != null) {
      //获取列表成功
      mList.clear();
      mList.addAll(original.getRows());
      CustomerProblemDealListAdapter mAdapter =
          new CustomerProblemDealListAdapter(getActivity(), mList);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    CustomerProblemDeal content = mList.get(position - 1);
    Intent intent = new Intent(getActivity(), CustomerProblemDealActivity.class);
    intent.putExtra(CustomerProblemDealFragment.EXTRA_CONTENT, content);
    intent.putExtra(CustomerProblemDealFragment.EXTRA_COMPLAINFAULTID, mContent.getId());
    intent.putExtra(CustomerProblemListFragment.EXTRA_MAINSTATUS, mContent.getDataStatus());
    startActivityForResult(intent, REQUEST_DETAIL);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_DETAIL) {
      volleyPost();
    }
  }

  public static CustomerProblemDealListFragment newInstance(CustomerProblem content) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, content);
    CustomerProblemDealListFragment fragment = new CustomerProblemDealListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
